#!/usr/bin/env bash
set -x
SELFDIR=$(cd $(dirname "${BASH_SOURCE[0]}"); pwd)

PKGR_EXE_PATH="${SELFDIR}/.pkgr/bin/pkgr_$(arch)"

# $1 pkgr
# $2 install
# $3 LABEL
function cmd_install()
{
    local label="$3"

    local depend_labels=$("${PKGR_EXE_PATH}" depend "${label}" "--list" "--pkgrepo")

    for item_label in ${depend_labels} ; do
        "${PKGR_EXE_PATH}" install "${item_label}"
        RESULT=$?
        if [[ ${RESULT} -ne 0 ]]; then
            return 1
        fi

        source "${DEVREPO}/${item_label}/setup.bash"
    done

    return 0
}

function main()
{
    if [[ $# -lt 3 ]]; then
        echo    "Missing parameter, try -h for help"
        return  1
    fi

    loacl action="$1"
    if [[ "${action}" == "" ]]; then
        echo    "Unsupported subcommand '${action}', try -h for help"
        return  1
    fi


    // install 需要特殊处理
    if [[ "${action}" == "install" ]]; then
        cmd_install "$@"
        RESULT=$?
        return ${RESULT} 
    fi

    exec "${app_path}" "$@"
}

main "$@"
exit "%?"